<?php

namespace App\Http\Controllers\Collect;

use App\Models\Collect;
use App\Models\Goods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CollectController extends Controller
{
    //加入收藏
    public function clickCollect(Request $request,Goods $good){
        $collect=Collect::where('user_id',auth()->id())->where('goods_id',$good->id)->first();
        if ($collect){
            return $request->expectsJson() ? ['valid'=>1,'message'=>'该商品已经收藏','shoucang' => 1] : back()->with('error','已经被收藏了');
        }else{
           $collects= Collect::create([
                'user_id'=>auth()->user()->id,
                'goods_id'=>$good->id,
            ]);
            return $request->expectsJson() ? ['valid'=>$collects,'message'=>'收藏成功','shoucang' => 1] : back()->with('success','已经添加到收藏中');
        }
        
       
//        return back()->with('success','已经添加到收藏中');
    }
    //取消收藏
    public function offCollect(Request $request,$good){
        Collect::where('goods_id',$good)->where('user_id',auth()->user()->id)->delete();
        return $request->expectsJson() ? ['valid'=>1,'message'=>'取消收藏成功','shoucang' => 0] : back()->with('success','取消收藏成功');
    }
    public function list(){
    $collects=Collect::where('user_id',auth()->user()->id)->with('getGoods')->get()->toArray();
    return view('collect.collect.list',compact('collects'));
    }
}
